// Xml.h: interface for the CXml class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_XML_H__A77B9829_2B3B_414F_8A0A_C60883CCB2DE__INCLUDED_)
#define AFX_XML_H__A77B9829_2B3B_414F_8A0A_C60883CCB2DE__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <AFXTEMPL.H>
#include <COMUTIL.H>

#import "msxml4.dll"
using namespace MSXML2;

#define _MAX_ARRAY	10

class CXmlNode
{
public:
	BOOL SetParamVal(_variant_t vtVal);
	_variant_t GetNodeVal(BOOL bOptional = FALSE);
	CString GetNodeText(BOOL bOptional = FALSE);
	CXmlNode GetNode(_bstr_t bstrNode);
	_variant_t GetNodeVal(CString strNode, BOOL bOptional = FALSE);
	_bstr_t GetXml();
	BOOL IsEmpty();
	CXmlNode GetNodeList(_bstr_t bstrNode);
	CXmlNode();
	void SetNode(IXMLDOMNodePtr node);
	void SetNodeList(IXMLDOMNodeListPtr list);
	CString GetNodeText(CString bstrNode, BOOL bOptional = FALSE);
	BOOL GetNextNode(CString& strName, CString& vtVal);
	BOOL GetNextNode();	
	CString GetAttribute(CString strName)
	{
			
		IXMLDOMNamedNodeMapPtr pNodeMapPtr=m_spDOMNode->attributes;
		IXMLDOMNodePtr attribute=pNodeMapPtr->getNamedItem(strName.AllocSysString());
		if(attribute==NULL) return _T("");
		CString strTemp=_T("");
		strTemp=(LPCSTR)attribute->Gettext(); 
		return strTemp;
	};
	BOOL ClearChildNodes()
	{
		ASSERT(m_spDOMNode!=NULL);
		try
		{
			if(m_spDOMNode->hasChildNodes())
			{
				
				CString strTemp=(LPCSTR)m_spDOMNode->GetnodeName();
				AfxMessageBox(strTemp);
				IXMLDOMNodeListPtr childNodeList=m_spDOMNode->GetchildNodes();
				while (childNodeList->Getlength()>0)
				{
					m_spDOMNode->removeChild(childNodeList->Getitem(0));
				}
			}	
		}
		catch (...) {
			return FALSE;
		}
		return TRUE;
	};
	
public:
	IXMLDOMNodeListPtr  m_spDOMNodeList;
	IXMLDOMNodePtr		m_spDOMNode;

protected:
		BOOL GetNodeVal(_bstr_t bstrNode, _variant_t& vtVal, BOOL bOptional = FALSE);
};

class CXml  
{
	class CStack
	{
	public:
		CStack()
		{
			m_nCount = 0;
		}
		~CStack()
		{
		}
		BOOL Push(IXMLDOMElementPtr item)
		{
			if(m_nCount >= _MAX_ARRAY)
				return FALSE;
			m_spaDOMElements[m_nCount] = item;
			m_nCount++;
			return TRUE;
		}
		BOOL Pop(IXMLDOMElementPtr& item)
		{
			if(m_nCount < 1)
				return FALSE;
			m_nCount--;
			item = m_spaDOMElements[m_nCount];
			return TRUE;
		}
		BOOL CanPop()
		{
			if(m_nCount < 1)
				return FALSE;
			return TRUE;
		}
		BOOL CanPush()
		{
			if(m_nCount >= _MAX_ARRAY)
				return FALSE;
			return TRUE;
		}

		int					m_nCount;
		IXMLDOMElementPtr	m_spaDOMElements[_MAX_ARRAY];
	};
public:
	BOOL CreateRoot(CString strRoot);
	BOOL AddXmlFragment(_bstr_t bstrXml);
	CXmlNode FindParamNode(_bstr_t bstrParentEle, CString strParamName);
	_variant_t GetNodeVal(CString strNode, BOOL bOptional = FALSE);
	BOOL AddParam(CString strName, _variant_t vtValue);
	CXmlNode GetNode(_bstr_t bstrNode);
	BOOL LoadXml(CString strFile);
	CXmlNode GetNodeList(_bstr_t bstrNode);
	CString GetNodeText(CString strNode, BOOL bOptional = FALSE);
	IXMLDOMDocument2Ptr GetDocumentPtr();
	BOOL SetXml(_bstr_t bstrXml);
	BOOL BackToParent();
	BOOL AddChildTextNode(CString strNodeName, _variant_t vtValue);
	BOOL AddChildElement(CString strElement);
	BOOL SaveXml(CString strFile);
	_bstr_t GetXml();
	BOOL CreateEPRRoot(CString strType);
	CString GetNodeAttText(CString strNode, CString strAtt, BOOL bOptional = FALSE);
	BOOL GetNodeAttVal(_bstr_t bstrNode,_bstr_t bstrAtt, _variant_t& vtVal, BOOL bOptional = FALSE);

	BOOL AddChildNode(CString strName,CString strAttributeName,CString strAttributeValue,CXmlNode* parentNode); 

	CXml();
	virtual ~CXml();

protected:
	IXMLDOMElementPtr	m_spDOMElement;
	BOOL				m_bHasInit;
	IXMLDOMDocument2Ptr	m_spDOMDocument;
	CStack				m_stackDOMElement;

protected:
	BOOL GetNodeVal(_bstr_t bstrNode, _variant_t& vtVal, BOOL bOptional = FALSE);
};

#endif // !defined(AFX_XML_H__A77B9829_2B3B_414F_8A0A_C60883CCB2DE__INCLUDED_)
